package com.makelove.bar.controller;

import com.makelove.bar.entity.User;
import com.makelove.bar.service.UserService;
import com.makelove.bar.util.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: bar
 * @description:
 * @author: 索腾龙
 * @create: 2021-04-28 17:30
 **/
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    //登录
    @RequestMapping("/Login")
    public String Login(User user, HttpSession httpSession,String userPassword){
        //用实体类接收userService的方法
        User users = userService.Login(user);
        //判断用户名是否存在
        if (users!=null){
            //判断输入的密码是否等于数据库的密码  如果相等
            if (user.getUserPassword().equals(userPassword)){
                //将获取的信息存入session里
                httpSession.setAttribute("users",users);
                //跳首页
                return "200";
            }else {
                return "201";
            }
        }else {
            return "201";
        }
    }
    //分页查找
    @GetMapping("getAllUser")
    public ViewObject<User> getAllUser(@RequestParam(value = "page")Integer page, @RequestParam(value = "limit")Integer limit){
        return userService.getAllUser(page, limit);
    }
    //添加
    @RequestMapping("/addUser")
    public String addUser(User user){
        int code = userService.addUser(user);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //修改
    @RequestMapping("/updUser")
    public String updUser(User user){
        int code = userService.updUser(user);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //删除
    @RequestMapping("/deleteUserById")
    public String deleteUserById(int id){
        int code = userService.del(id);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }

}